# -*- coding:utf-8 -*-
"""
@Author : 审心
@Time : 2023/3/27 15:17
查找竞品
"""
import random
import time
from dataclasses import dataclass, field
from typing import Optional, List

import pyautogui
import pyperclip
from DrissionPage import WebPage
from autoTask.taobao.sycm.gjc.competitorGoodKey.publicResource import GoodsFeature, __links as links, \
    wait_tb_complete
from autoTask.taobao.sycm.goods.findMarketRank import FindMarketRank, GetDataAction, FilterAction
from common.logger.log import log_
from common.utils import ExcelUtil, web_page_util, StrUtil, ObjUtil
from common.utils.web_page_util import create_page, MockTap


@dataclass
class FindCompetingProducts:
    """
    根据品类名称获取本店商品与其竞品映射的列表
    :param page: 页面对象
    :param goods_features: 本店商品特征
    :param category: 品类
    :param date_cycle: 数据周期(7天 或 30天)
    :param contain_name_list: 忽略不包含指定字符串的商品名称文本列表 元素 ['平底锅']
    :param ignore_name_list: 忽略包含指定字符串的商品名称文本列表 元素 ['平底锅']
    :param goods_rank_list 网页需求数据
    """
    page: WebPage
    goods_features: List[GoodsFeature]
    category: str
    date_cycle: str
    contain_name_list: List[str] = field(default_factory=list)
    ignore_name_list: List[str] = field(default_factory=list)
    goods_rank_list: List = field(default_factory=list)

    def __call__(self) -> list[GoodsFeature]:
        """
        执行本文件过程
        :return 返回本店商品与竞品的映射字典 {'category':平底锅,'my':1,'competing1': 2,'competing2':3}
        :rtype : list[GoodsFeature]
        """
        # 对商品映射设置数据周期以便后续使用
        for item in self.goods_features:
            item.date_cycle = self.date_cycle
        # 获取需要设置竞品的目标
        need_set_goods = [goods for goods in self.goods_features
                          if StrUtil.any_blank(goods.competing1, goods.competing2)]
        # 获取不需要设置竞品的目标为直接返回的结果
        result = [goods for goods in self.goods_features
                  if StrUtil.non_any_blank(goods.competing1, goods.competing2)]
        if len(need_set_goods) > 0:
            # 对没有竞品的商品设置竞品
            result += self.set_competing_id(need_set_goods)
        return result

    def set_competing_id(self, need_set_goods: List[GoodsFeature]):
        """
        设置竞品id
        :param need_set_goods 需要设置竞品的商品
        :return:返回竞品编号列表
        :rtype : list[GoodsFeature]
        """
        # 获取商品排行列表
        if ObjUtil.is_empty(self.goods_rank_list):
            self.goods_rank_list = GetDataAction(category=self.category, date_cycle=self.date_cycle)(p=self.page)
        # 对商品设置竞品
        for item in need_set_goods:
            # 根据条件过滤排行列表
            c_list = FilterAction(price_scope=item.price_scope, title_contain_str=self.contain_name_list,
                                  title_not_contain_str=self.ignore_name_list)(self.goods_rank_list)
            if len(c_list) < 2:
                log_.warning(f"获取品类 : {self.category} 竞品失败 "
                             f"\n 客单价范围: {item.price_scope} "
                             f"\n 标题必须包含的字符: {self.contain_name_list}"
                             f"\n 标题不能包含的字符: {self.ignore_name_list}"
                             f"\n 竞品列表: {c_list}")
                continue
            # 交易金额降序
            c_list.sort(key=lambda t: t[9], reverse=True)
            item.competing1 = c_list[0][6]
            item.competing2 = c_list[1][6]
        return need_set_goods


if __name__ == '__main__':
    page_ = create_page()
    log_.info(page_.url)
